<?php

namespace app\commons\controller;

use think\Controller;
use think\Db;
use org\Upload;
use think\Exception;

class Upfile extends Controller {
    
    
	public function upload()
	{
	     require_once '../extend/org/Upload.php';
	     $image_storage = [
            'type' => 'Local',
        ];

        $config = [
            //'subName' => ['get_date_dir', ''],
            'exts' => 'jpg,jpeg,png,gif,mp4',
            'saveName' => ['uniqid', time()],
        ];
        $upload = new \org\upload($config, "Local", $image_storage);
        $fileinfo = $upload->upload($_FILES);
        $dest = [
            'hash' => $fileinfo['file']['md5'],
            'key' => './Uploads/' . $fileinfo['file']['savepath'] . $fileinfo['file']['savename'],
            'ext' => $fileinfo['file']['ext']
        ];
        $res = array();
        $data = pdo_get('images', array('hash' => $dest['hash']));
        if ($data) {
            if (file_exists($data['key'])) {
                unlink($dest['key']);
                $res[]= "/".$data['key'];
            } else {
                $res[]="/". $dest['key'];
            }
        }
		header('Content-Type:application/json; charset=utf-8');
		echo json_encode(array('errno' => 0, 'data' => $res));
		exit;
	}
    

    public function index() {
        require_once '../extend/org/Upload.php';
        $image_storage = [
            'type' => 'Local',
        ];

        $config = [
            //'subName' => ['get_date_dir', ''],
            'exts' => 'jpg,jpeg,png,gif,mp4',
            'saveName' => ['uniqid', time()],
        ];
        $upload = new \org\upload($config, "Local", $image_storage);
        $fileinfo = $upload->upload($_FILES);
        $dest = [
            'hash' => $fileinfo['file']['md5'],
            'key' => './Uploads/' . $fileinfo['file']['savepath'] . $fileinfo['file']['savename'],
            'ext' => $fileinfo['file']['ext']
        ];
        unset($dest['ext']);
        $data = pdo_get('images', array('hash' => $dest['hash']));
        if ($data) {
            if (file_exists($data['key'])) {
                unlink($dest['key']);
                return jsonp(array('hash' => $data['hash'], 'key' => $data['key']));
            } else {
                pdo_update('images', array('key' => $dest['key']), array('id' => $data['id']));
                return jsonp($dest);
            }
        } else {
            pdo_insert('images', $dest);
            return jsonp($dest);
        }
    }

    /***
     * 上传常规视频
     * @return \think\response\Jsonp
     */
    public function mp4s() {
        require_once '../extend/org/Upload.php';
        $image_storage = [
            'type' => 'Local',
        ];

        $config = [
            //'subName' => ['get_date_dir', ''],
            'exts' => 'mp4',
            'saveName' => ['uniqid', time()],
        ];
        $upload = new \org\upload($config, "Local", $image_storage);
        $fileinfo = $upload->upload($_FILES);
        $dest = [
            'hash' => $fileinfo['file']['md5'],
            'key' => './Uploads/' . $fileinfo['file']['savepath'] . $fileinfo['file']['savename'],
            'ext' => $fileinfo['file']['ext']
        ];
        unset($dest['ext']);
        $data = pdo_get('videos', array('hash' => $dest['hash']));
        if ($data) {
            if (file_exists($data['key'])) {
                unlink($dest['key']);
                jsont(array('hash' => $data['hash'], 'key' => $data['key']));
            } else {
                pdo_update('videos', array('key' => $dest['key']), array('id' => $data['id']));
                jsont($dest);
            }
        } else {
            pdo_insert('videos', $dest);
            jsont($dest);
        }
    }

    public function mp4() {
        require_once '../extend/org/Upload.php';
        $image_storage = [
            'type' => 'Local',
        ];

        $config = [
            //'subName' => ['get_date_dir', ''],
            'exts' => 'mp4',
            'saveName' => ['uniqid', time()],
        ];
        $upload = new \org\upload($config, "Local", $image_storage);
        $fileinfo = $upload->upload($_FILES);
        $dest = [
            'hash' => $fileinfo['file']['md5'],
            'key' => './Uploads/' . $fileinfo['file']['savepath'] . $fileinfo['file']['savename'],
            'ext' => $fileinfo['file']['ext']
        ];
        unset($dest['ext']);
        $data = pdo_get('videos', array('hash' => $dest['hash']));
        if ($data) {
            if (file_exists($data['key'])) {
                unlink($dest['key']);
                return jsonp(array('hash' => $data['hash'], 'key' => $data['key']));
            } else {
                pdo_update('videos', array('key' => $dest['key']), array('id' => $data['id']));
                return jsonp($dest);
            }
        } else {
            pdo_insert('videos', $dest);
            return jsonp($dest);
        }
    }

    public function media() {
        require_once '../extend/org/Upload.php';
        $image_storage = [
            'type' => 'Local',
        ];
        try {
            $config = [
                'exts' => ['mp4', 'mp3', 'jpeg', 'jpg', 'png'],
                'saveName' => ['uniqid', time()],
            ];
            $upload = new \org\upload($config, "Local", $image_storage);
            $fileinfo = $upload->upload($_FILES);
            $dest = [
                'hash' => $fileinfo['file']['md5'],
                'key' => './Uploads/' . $fileinfo['file']['savepath'] . $fileinfo['file']['savename'],
                'ext' => $fileinfo['file']['ext']
            ];

            if ($dest['ext'] == 'mp4') {
                $table = 'videos';
            } else {
                $table = 'images';
            }
            $data = pdo_get($table, array('hash' => $dest['hash']));
            unset($dest['ext']);
            if ($data) {
                if (file_exists($data['key'])) {
                    unlink($dest['key']);
                    jsont(['hash' => $data['hash'], 'key' => $data['key']],0,'上传成功');
//                    json(array());
                } else {
                    pdo_update($table, array('key' => $dest['key']), array('id' => $data['id']));
                    jsont($dest,0,'上传成功');
                }
            } else {
                pdo_insert($table, $dest);
                return jsont($dest,0,'上传成功');
                return jsonp($dest);
            }
        }catch (Exception $exception){
            return jsont([],1,$exception->getMessage());
        }

    }

}
